#include <stdio.h>
#include <string.h>
#include "math_statistics_func.h"
#include "eigenvalue_calc.h"
#include "fftw.h"
#include "win_func.h"
#include "calc_type.h"
#include "usr_cfg.h"


static float abs_tmp[RAW_DATA_SIZE];
static float sqr_tmp[RAW_DATA_SIZE];

void acc_td_eigenvalue_calc(sys_cfg_info *sys, acc_unit acc[], chx_cfg_info chx[])
{	
	uint32_t index, data_len;
	
	float std, rms, mean, abs_mean, peak, max, min, sqr_root;
	
	data_len = sys->samp_freq * sys->duration;

	//#pragma omp parallel for
	for (uint8_t ch = 0; ch < ACC_CH_TOTAL; ch++) {		
		if (chx[ch].chx_en == ON) {					//只计算使能的通道
			if (sys->eigen_calc == ON) {
				arm_mean_f32(acc[ch].td_vdata_raw_g, data_len, &mean);
				acc[ch].td_eigen_value.td_mean = mean;
				arm_abs_f32(acc[ch].td_vdata_raw_g, abs_tmp, data_len);
				arm_mean_f32(abs_tmp, data_len, &abs_mean);
				acc[ch].td_eigen_value.td_abs_mean = abs_mean;
				for(uint32_t i = 0; i < data_len; i++)
  					sqr_tmp[i] = sqrtf(abs_tmp[i]);
				arm_sqr_root_f32(sqr_tmp, data_len, &sqr_root);
				acc[ch].td_eigen_value.td_sqr_root = sqr_root;
				arm_rms_f32(acc[ch].td_vdata_raw_g, data_len, &rms);
				acc[ch].td_eigen_value.td_rms = rms;
				arm_max_f32(acc[ch].td_vdata_raw_g, data_len, &max, &index);
				arm_min_f32(acc[ch].td_vdata_raw_g, data_len, &min, &index);
				arm_max_f32(abs_tmp, data_len, &peak, &index);
				acc[ch].td_eigen_value.td_peak = peak;
				acc[ch].td_eigen_value.td_ppeak = max - min;
				acc[ch].td_eigen_value.td_wave_factor = rms / abs_mean;
				acc[ch].td_eigen_value.td_crest_factor = peak / rms;
				acc[ch].td_eigen_value.td_pulse_factor = peak / abs_mean;
				acc[ch].td_eigen_value.td_margin_factor = peak / sqr_root;
				arm_std_f32(acc[ch].td_vdata_raw_g, data_len, mean, &std);											
				acc[ch].td_eigen_value.td_skewness = skewf_neon(acc[ch].td_vdata_raw_g, data_len, mean, std);
				acc[ch].td_eigen_value.td_kurtosis = kurtosisf_neon(acc[ch].td_vdata_raw_g, data_len, mean, std);
				acc[ch].td_eigen_value.td_peakvue_max = 0.0f;
				acc[ch].td_eigen_value.td_peakvue_min = 0.0f;
				acc[ch].td_eigen_value.td_peak_vel = 0.0f;
				acc[ch].td_eigen_value.td_rms_vel = 0.0f;
			} else {
				arm_fill_f32(0.0, &(acc[ch].td_eigen_value.td_mean), (sizeof(td_calc_norl) / sizeof(float)));
			}
			if (sys_cfg.debug == ON) {
				printf("**************************************************************\n");
				printf("acc[%d].td_eigen_value.td_mean is		%f\n", ch, mean);
				printf("acc[%d].td_eigen_value.td_abs_mean		%f\n", ch, abs_mean);
				printf("acc[%d].td_eigen_value.td_sqr_root is		%f\n", ch, sqr_root);
				printf("acc[%d].td_eigen_value.td_rms is			%f\n", ch, rms);
				//printf("acc[%d].td_eigen_value.td_max is 		%f\n", ch, max);
				//printf("acc[%d].td_eigen_value.td_min is 		%f\n", ch, min);
				printf("acc[%d].td_eigen_value.td_peak is 		%f\n", ch, peak);
				printf("acc[%d].td_eigen_value.td_ppeak is 		%f\n", ch, max - min);
				printf("acc[%d].td_eigen_value.td_wave_factor is 	%f\n", ch, rms / mean);
				printf("acc[%d].td_eigen_value.td_crest_factor is 	%f\n", ch, peak / rms);
				printf("acc[%d].td_eigen_value.td_pulse_factor is 	%f\n", ch, peak / mean);
				printf("acc[%d].td_eigen_value.td_margin_factor is 	%f\n", ch, peak / sqr_root);
				printf("acc[%d].td_eigen_value.td_skewness is		%f\n", ch, 
							acc[ch].td_eigen_value.td_skewness);
				printf("acc[%d].td_eigen_value.td_kurtosis is		%f\n", ch, 
							acc[ch].td_eigen_value.td_kurtosis);
				printf("acc[%d].td_eigen_value.td_peakvue_max is 	%f\n", ch, 
							acc[ch].td_eigen_value.td_peakvue_max);
				printf("acc[%d].td_eigen_value.td_peakvue_min is 	%f\n", ch, 
							acc[ch].td_eigen_value.td_peakvue_min);
				printf("acc[%d].td_eigen_value.td_peak_vel is		%f\n", ch, 
							acc[ch].td_eigen_value.td_peak_vel);
				printf("acc[%d].td_eigen_value.td_rms_vel is		%f\n", ch, 
							acc[ch].td_eigen_value.td_rms_vel);
				printf("**************************************************************\n");
			}
		} else {
			arm_fill_f32(0.0, &(acc[ch].td_eigen_value.td_mean), (sizeof(td_calc_norl) / sizeof(float)));
		}
	}
}



